package com.rtwo.tio.server.handler;

import com.rtwo.tio.client.msg.GroupMsgRequest;
import com.rtwo.tio.common.BasicHandler;
import com.rtwo.tio.common.BasicPacket;
import com.rtwo.tio.common.MsgType;
import com.rtwo.tio.server.msg.GroupMsgResponse;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.json.Json;

import java.io.UnsupportedEncodingException;

/**
 * Created by hasee on 2018/7/20.
 */
public class GroupMsgResponseHandler extends BasicHandler<GroupMsgRequest> {
    @Override
    public Class<GroupMsgRequest> getClazz() {
        return GroupMsgRequest.class;
    }

    @Override
    public void postHanlder(GroupMsgRequest body, ChannelContext channelContext) throws UnsupportedEncodingException {
        String toGroup = body.getToGroup();
        String fromUser = (String) channelContext.getAttribute("username");
        String fromGroup = (String) channelContext.getAttribute("groupName");
        GroupMsgResponse groupMsgResponse = new GroupMsgResponse(fromUser,body.getMsg(),fromGroup);
        String groupMsgResponseJsonStr = Json.toJson(groupMsgResponse);
        Tio.sendToGroup(channelContext.getGroupContext(),toGroup,new BasicPacket(MsgType.GROUP_MSG_RESP,groupMsgResponseJsonStr.getBytes()));
    }
}
